package net.lainiao.zhihu.control.front;

import net.lainiao.zhihu.common.MyPage;
import net.lainiao.zhihu.model.Zhihudaan;
import net.lainiao.zhihu.model.Zhihuhuati;
import net.lainiao.zhihu.model.ZhihuwentiWithBLOBs;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import net.lainiao.zhihu.service.ZhihudananService;
import net.lainiao.zhihu.service.ZhihuhuatiService;
import net.lainiao.zhihu.service.ZhihuwentiService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2016/1/18.
 */
@Controller
@RequestMapping(value = "/front/duanzi")
public class DuanziControl {

    @Resource
    private ZhihuhuatiService zhihuhuatiService;

    @Resource
    private ZhihuwentiService zhihuwentiService;

    @Resource
    private ZhihudananService zhihudananService;

    @RequestMapping(method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        List<Zhihuhuati> list = zhihuhuatiService.getList();
        modelMap.put("list", list);
        return "front/duanzi";
    }

    @RequestMapping(value = "/page/{pageid}")
    public String page(@PathVariable("pageid") String pageid, HttpServletRequest request, ModelMap modelMap) {
        String pageindex = request.getParameter("pageindex");
        if (StringUtils.isEmpty(pageindex)) {
            pageindex = "1";
        }
        MyPage myPage = new MyPage();
        myPage.setCurrentPage(Integer.parseInt(pageindex));
        myPage.setPageSize(20);
        myPage.setObject(pageid);
        List<ZhihuwentiWithBLOBs> zhihuwentis = zhihuwentiService.getListByhuatiIdPage(myPage);
        modelMap.put("list", zhihuwentis);
        modelMap.put("total", myPage.getTotalPage());
        modelMap.put("pageid", pageid);
        return "front/duanzipage";
    }

    @RequestMapping(value = "/wenti/{wentiid}")
    public String wenti(@PathVariable("wentiid") String wentiid, ModelMap modelMap) {
        ZhihuwentiWithBLOBs zhihuwentiWithBLOBs = zhihuwentiService.getwentiById(wentiid);
        modelMap.put("wenti", zhihuwentiWithBLOBs);
        List<Zhihudaan> zhihudaans = zhihudananService.getDanans(wentiid);
        modelMap.put("daans", zhihudaans);
        return "front/duanziwenti";
    }

}
